package com.yc.cloud.openai.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yc.cloud.common.basic.exception.Asserts;
import com.yc.cloud.openai.portal.common.PortalErrorTips;
import com.yc.cloud.openai.portal.dao.GPTChatClient;
import com.yc.cloud.openai.portal.dto.request.ChatCompletionsRequest;
import com.yc.cloud.openai.portal.dto.request.SpeechToTextRequest;
import com.yc.cloud.openai.portal.enums.GPTChatProvider;
import com.yc.cloud.openai.portal.enums.SpeechToTextProvider;
import com.yc.cloud.openai.portal.service.AudioService;
import com.yc.cloud.openai.portal.service.GPTChatService;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;

@Service
public class GPTChatClientImpl implements GPTChatService {

    private final Map<GPTChatProvider, GPTChatClient> chatClients = new EnumMap<>(GPTChatProvider.class);

    private final AudioService audioService;

    public GPTChatClientImpl(Map<String, GPTChatClient> chatClientMap, AudioService audioService) {
        this.audioService = audioService;
        // 使用枚举的 value 作为键来初始化 Map
        chatClientMap.forEach((name, client) -> chatClients.put(GPTChatProvider.valueOf(name), client));
    }

    @Override
    public Flux<String> completionsByStream(ChatCompletionsRequest request, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            val text = convertSpeechToText(file);
            request.setMessage(text);
        }
        if (StrUtil.isEmpty(request.getMessage())) {
            Asserts.fail(PortalErrorTips.CHAT_CONTENT_EMPTY);
        }
        val provider = GPTChatProvider.fromKey(request.getProvider());
        // 使用 Optional 查找客户端，如果未找到，则使用默认客户端
        val client = Optional.ofNullable(chatClients.get(provider))
                .orElse(getDefaultGptChatClient());
        return client.completionsByStream(request);

    }

    @Override
    public String completions(ChatCompletionsRequest request, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            val text = convertSpeechToText(file);
            request.setMessage(text);
        }
        val provider = GPTChatProvider.fromKey(request.getProvider());
        // 使用 Optional 查找客户端，如果未找到，则使用默认客户端
        val client = Optional.ofNullable(chatClients.get(provider))
                .orElse(getDefaultGptChatClient());
        return client.completions(request);
    }

    // 语音文件提取出文本信息
    private String convertSpeechToText(MultipartFile file) {
        val speechToTextRequest = new SpeechToTextRequest();
        speechToTextRequest.setProvider(SpeechToTextProvider.XUN_FEI.getKey());
        val speechToTextResponse = audioService.convertSpeechToText(file, speechToTextRequest);
        if (speechToTextResponse == null) {
            return "";
        }
        return speechToTextResponse.getText();
    }

    private GPTChatClient getDefaultGptChatClient() {
        return chatClients.get(GPTChatProvider.DEFAULT);
    }

}
